home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / whttpd14.zip / SUPPORT / LOGCYCLE / GETOPT.C next >
C/C++ Source or Header  |  1994-06-05  |  3KB  |  90 lines

  1. //
  2. // Here's something you've all been waiting for:  the AT&T public domain
  3. // source for getopt(3).  It is the code which was given out at the 1985
  4. // UNIFORUM conference in Dallas.  I obtained it by electronic mail
  5. // directly from AT&T.  The people there assure me that it is indeed
  6. // in the public domain.
  7. //
  8. // There is no manual page.  That is because the one they gave out at
  9. // UNIFORUM was slightly different from the current System V Release 2
  10. // manual page.  The difference apparently involved a note about the
  11. // famous rules 5 and 6, recommending using white space between an option
  12. // and its first argument, and not grouping options that have arguments.
  13. // Getopt itself is currently lenient about both of these things White
  14. // space is allowed, but not mandatory, and the last option in a group can
  15. // have an argument.  That particular version of the man page evidently
  16. // has no official existence, and my source at AT&T did not send a copy.
  17. // The current SVR2 man page reflects the actual behavor of this getopt.
  18. // However, I am not about to post a copy of anything licensed by AT&T.
  19. //
  20. //    -- Andrew Derbyshire
  21. //
  22. #include <stdio.h>
  23. #include <string.h>
  24. #define  index strchr
  25.  
  26. #include "getopt.h"
  27.  
  28. #define ERR(s,c) fprintf(stderr,"%s%s%c\n", argv[0],s,c)
  29.  
  30. //
  31. // Globals used by the caller
  32. //
  33. int   optind = 1;
  34. int   optopt;
  35. char  *optarg;
  36.  
  37. int getopts(int argc, char **argv, char *opts)
  38. {
  39.    static int sp = 1;
  40.    register int c;
  41.    register char *cp;
  42.  
  43.    if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == '\0')
  44.        if((cp=index(opts, '-')) != NULL) {
  45.            optind++;
  46.            return('-');
  47.        } else {
  48.            optind++;
  49.            return('?');
  50.        }
  51.  
  52.    if(sp == 1)
  53.       if(optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
  54.          return(EOF);
  55.       else if(strcmp(argv[optind], "--") == 0) {
  56.          optind++;
  57.          return(EOF);
  58.       }
  59.  
  60.    optopt = c = argv[optind][sp];
  61.  
  62.    if(c == ':' || (cp=index(opts, c)) == NULL) {
  63.       ERR(": illegal option -- ", c);
  64.       if(argv[optind][++sp] == '\0') {
  65.          optind++;
  66.          sp = 1;
  67.       }
  68.       return('?');
  69.    }
  70.  
  71.    if(*++cp == ':') {
  72.       if(argv[optind][sp+1] != '\0')
  73.          optarg = &argv[optind++][sp+1];
  74.       else if(++optind >= argc) {
  75.          ERR(": option requires an argument -- ", c);
  76.          sp = 1;
  77.          return('?');
  78.       } else
  79.          optarg = argv[optind++];
  80.       sp = 1;
  81.    } else {
  82.       if(argv[optind][++sp] == '\0') {
  83.          sp = 1;
  84.          optind++;
  85.       }
  86.       optarg = NULL;
  87.    }
  88.    return(c);
  89. }
  90.